package com.zhangck.bos_fore.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;





/** 
* @author 上午茶
* @version 创建时间：2018年6月8日 上午11:22:51 
* 类说明 
*/
@SuppressWarnings("all")
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
@Service("permissionAction")
public class PromotionAction extends BaseAction<Promotion> implements MessageListener {
		
	@Action(value="promotion_page",results={@Result(name="success", type="json")})
	public String promotion_page(){
		
		PageBean<Promotion> promotion = WebClient.create("http://localhost:8080/bos_management/services/promotionService/findPage?page="+page+"&rows="+rows).accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		
		ActionContext.getContext().getValueStack().push(promotion);
		
		return "success";
	}
	
	@Action(value="promotion_Showdetail")
	public String promotion_Showdetail() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{
	
		String htmlURL  = ServletActionContext.getServletContext().getRealPath("/framMraker");
		File file = new File(htmlURL+File.separator+model.getId()+".html");
		//如果文件不存在，查询数据库，生成framMraker
		if (!file.exists()) {
			String realPath = ServletActionContext.getServletContext().getRealPath("");
		
			//配置对象，配置模板位置
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
				configuration.setDirectoryForTemplateLoading(new File(
						ServletActionContext.getServletContext().getRealPath("/WEB-INF/framMraker_templates")));
				
				//获取模板 
				Template template = configuration.getTemplate("promotion_Show.ftl");
				System.out.println(model.getId());
				//动态数据
				Promotion promotion = WebClient.create("http://localhost:8080/bos_management/services/promotionService/findPromotion?id="+model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			
				Map<String, Object> map = new HashMap<>();
				map.put("promotion", promotion);
				template.process(map, new OutputStreamWriter(
				new FileOutputStream(file),"utf-8"));
		
		}
		try {
		ServletActionContext.getResponse().setContentType("text/html:charset=utf-8");
			FileUtils.copyFile(file, ServletActionContext.getResponse().getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return NONE;
	}

	@Override
	public void onMessage(Message message) {
		try {
		MapMessage mapMessage = (MapMessage) message;
			if ("true".equals(mapMessage.getString("falg"))) {
				String htmlURL = "D:/temp/bos_2.0_ssh/bos_fore/src/main/webapp/framMraker";
				File file = new File(htmlURL+File.separator+mapMessage.getString("id")+".html");
				System.out.println("删除了该文件~~~");
				if (file.delete()) {
					System.out.println("删除成功");
				}else {
					System.out.println("删除失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
